﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace ZX.Web.Message
{
    public class MailThread
    {
        private MailMessage mail;
        private string fromAddress = "";
        private string userName = "";
        private string passWord = "";
        private string smtpServer = "smtp.sohu.com";
        public string bodyContent = "";
        public string subject = "";
        public string toAddress;
        public string messageId = "";

        public MailThread()
        {
           
            mail = new MailMessage();
            fromAddress = System.Configuration.ConfigurationSettings.AppSettings["FromAddress"];
            userName = System.Configuration.ConfigurationSettings.AppSettings["EmailUsername"];
            passWord = System.Configuration.ConfigurationSettings.AppSettings["EmailPassword"];
            smtpServer = System.Configuration.ConfigurationSettings.AppSettings["Smtp"];
        }

        public void Send()
        {
            lock (this)
            {
                mail.From = new MailAddress(fromAddress);
                if (toAddress.Length > 0)
                {
                    string[] ad = toAddress.Split(',');
                    foreach (string s in ad)
                    {
                        mail.To.Add(s);
                    }
                }
                mail.Subject = subject;
                mail.Body = bodyContent;
                SmtpClient smtp = new SmtpClient(smtpServer);
                smtp.Credentials = new NetworkCredential(userName, passWord);

                try
                {
                    smtp.Send(mail);
                }
                catch (Exception ex)
                {
                    throw ex;

                }
            }
        }
    }
}
